package com.bong.panpan.bongcharttools.custom;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * Created by panpan on 2017/11/3.
 */

public class PPGECGRecord implements Serializable {
    public UserInfoRecord userInfoRecord;//用户信息储存
    public ArrayList<int[]> chartData;//PPG、ECG曲线数据
    public int[] values;//高压低压心率值
    public int measureTime=0;//测量时间

    public static boolean save(PPGECGRecord record, String path) {
        ObjectOutputStream outputStream = null;
        try {
            outputStream = new ObjectOutputStream(new FileOutputStream(path));
            outputStream.writeObject(record);
            outputStream.close();
            return saveToCSV(record, path.replace(Constant.chartEndName, ".csv"));
        } catch (Exception e) {
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (Exception e1) {

            }
        }
        return false;
    }

    public static PPGECGRecord load(String path) {
        PPGECGRecord record = null;
        ObjectInputStream inputStream = null;
        try {

            inputStream = new ObjectInputStream(new FileInputStream(path));
            record = (PPGECGRecord) inputStream.readObject();
            inputStream.close();
        } catch (Exception e) {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (Exception e1) {

            }
        }
        return record;
    }

    private static boolean saveToCSV(PPGECGRecord record, String path) {
        BufferedWriter outputStream = null;
        try {
            outputStream = new BufferedWriter(new FileWriter(path));
            //outputStream=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "ANSI"));
            StringBuffer stringBuffer = new StringBuffer();
            if (record.userInfoRecord != null)
                stringBuffer.append(record.userInfoRecord.toString());
            stringBuffer.append("\n");
            stringBuffer.append("\n手环测量数据");
            stringBuffer.append("\n高压,低压,心率,测量时间\n");
            if (record.values != null) {
                stringBuffer.append(record.values[0] +
                        "," + record.values[1] +
                        "," + record.values[2]+
                        "," + String.format("%02d:%02d.%03d",record.measureTime/1000/60,record.measureTime/1000%60,record.measureTime%1000)
                );
            } else {
                stringBuffer.append("无数据,无数据,无数据");
            }
            stringBuffer.append("\n");
            String info[] = stringBuffer.toString().split("\n");
            int infoIndex = 0;
            stringBuffer.delete(0, stringBuffer.length());
            stringBuffer.append("\uFEFF");//有BOM的UTF-8编码
            stringBuffer.append("PPG,ECG" + ",," + info[infoIndex++]);
            outputStream.write(stringBuffer.toString());
            outputStream.newLine();
            int[] temp;
            for (int i = 0; i < record.chartData.size(); i++) {
                temp = record.chartData.get(i);
                stringBuffer.delete(0, stringBuffer.length());
                for (int j = 0; j < temp.length; j += 2) {
                    if (j != 0)
                        stringBuffer.append("\n");
                    if (infoIndex < info.length)
                        stringBuffer.append(MyUtil.uint2Int(temp[j]) + "," + MyUtil.uint2Int(temp[j + 1]) + ",," + info[infoIndex++]);
                    else
                        stringBuffer.append(MyUtil.uint2Int(temp[j]) + "," + MyUtil.uint2Int(temp[j + 1]));
                }
                outputStream.write(stringBuffer.toString());
                outputStream.newLine();
            }
            outputStream.close();
            return true;
        } catch (Exception e) {
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (Exception e1) {

            }
        }
        return false;
    }
}
